<?php
namespace app\channel\controller;
use think\Db;

class DataChannelGame extends Base
{
    public function index(){
        if(request()->isAjax() || input('?export')){
            $page = input('page');
            $limit = input('limit');
            $plat = input('plat');
            $date = explodeTime(input('date'));
            $channelId = input('channel_id');
            $gameId = input('game_id');


            if($plat=='cps'){
                !session('admin.cps_id') ? $this->layTable(0, '', [], 0) : $id=session('admin.cps_id');
            }else{
                !session('admin.sdk_id') ? $this->layTable(0, '', [], 0) : $id=session('admin.sdk_id');
            }

            if( strtotime(date('Y-m-d', $date['end_time'])) == strtotime(date('Y-m-d', time())) ){
                $selectChannel = model('manage/Data')->selectChannelGameNow($page, $limit, $plat, $date['start_time'], $date['end_time'], $channelId, $gameId, $id);
            }else{
                $selectChannel = model('manage/Data')->selectChannelGameOld($page, $limit, $plat, $date['start_time'], $date['end_time'], $channelId, $gameId, $id);
            }
            if(input('?export')){
                array_unshift($selectChannel['data'], ['统计日期', '渠道ID', '渠道名称', '游戏ID', '游戏名称', '游戏新增', '游戏创角', '游戏活跃', '付费人数', '付费金额', '付费率', '新增用户ARPU', '活跃用户ARPU', '付费用户ARPU']);
                exportExcel($selectChannel['data'], input('date').'渠道游戏统计', input('date').'渠道游戏统计');
            }

            $this->layTable(0, '渠道游戏统计', $selectChannel['data'], $selectChannel['count'], $selectChannel['sum']);
        }else{
            $date = date('Y-m-d', time()-86400).' - '.date('Y-m-d', time()-86400);
            $today = date('Y-m-d', time()).' - '.date('Y-m-d', time());
            $channelData = model('Base')->channelNotice();
            $gameData = model('Base')->gameNotice();

            if(session('admin.cps_id') && session('admin.sdk_id')){
                $cpsSelected = 'selected';
                $sdkSelected = '';
            }else if(session('admin.cps_id')){
                $cpsSelected = 'selected';
                $sdkSelected = '';
            }else if(session('admin.sdk_id')){
                $cpsSelected = '';
                $sdkSelected = 'selected';
            }
            return view('', ['date'=>$date, 'today'=>$today, 'channel_data'=>$channelData, 'game_data'=>$gameData, 'cps_selected'=>$cpsSelected, 'sdk_selected'=>$sdkSelected]);
        }
    }
}